const fetch = require('./fetch')
const config = require('../config')
const API = "https://api.douban.com/v2/movie/"


const baseParams = {
    apikey: config.apikey,
    city: config.city
};


/**
 * path: String    请求地址
 * params: Object  请求参数
 */

function fetchData(path, params) {
    return fetch(API + path, params);
}

/**
 * start: Number    分页使用，表示第几页
 * count: Number    分页使用，表示数量
 * return promise
 */
function getMovies(start, count) {
    const params = {
        start: start || 0,
        count: count || 10
    }

    return fetchData('in_theaters', Object.assign(baseParams, params)).then(res => res.data)
}

/**
 * id: Number   电影的ID
 * return promise
 */
function getMovie(id) {
    return fetchData('subject/' + id, baseParams)
}

module.exports = { getMovies, getMovie }